package com.example.pro1.service.zhao.table.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.pro1.entity.zhao.OrderInfo;
import com.example.pro1.entity.zhao.Product;
import com.example.pro1.enums.OrderStatus;
import com.example.pro1.mapper.zhao.OrderInfoMapper;
import com.example.pro1.mapper.zhao.ProductMapper;
import com.example.pro1.service.zhao.table.OrderInfoService;
import com.example.pro1.util.zhao.OrderNoUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.Duration;
import java.time.Instant;
import java.util.Date;
import java.util.List;

@Service
public class OrderInfoServiceImpl extends ServiceImpl<OrderInfoMapper, OrderInfo> implements OrderInfoService {


    @Resource
    private ProductMapper productMapper;
    @Override
    public OrderInfo createOderByProductId(Long productId,String chargeWay,Long userId,Integer fee,String num,String beizhu,String addressId) {
        Product product =productMapper.selectById(productId);

        //生成订单
        //调用下单Api
        OrderInfo orderInfo=new OrderInfo();
        //订单标题
        orderInfo.setTitle(product.getGname());
        //订单编号
        orderInfo.setOrderNo(OrderNoUtils.getOrderNo());
        //商品id
        orderInfo.setProductId(productId);
        //金额
        orderInfo.setTotalFee( fee);//分
        //支付状态
        orderInfo.setOrderStatus(OrderStatus.NOTPAY.getType());
        //UerId
        orderInfo.setUserId(userId);
        //地址ID
        orderInfo.setAddressId(addressId);
        orderInfo.setCreateTime(new Date());
        //支付方式
        orderInfo.setChargeWay(chargeWay);
        orderInfo.setCodeUrl("dadsad");
        orderInfo.setNum(num);
        orderInfo.setBeizhu(beizhu);
        baseMapper.insert(orderInfo);
        return orderInfo;

    }

    public void saveCode_url(OrderInfo orderInfo)
    {
        QueryWrapper<OrderInfo> orderInfoQueryWrapper = new QueryWrapper<>();
        orderInfoQueryWrapper.eq("orderNum",orderInfo.getOrderNo());
        baseMapper.update(orderInfo,orderInfoQueryWrapper);
    }

    @Override
    public List<OrderInfo> findAllByCreatTimeDesc() {

        QueryWrapper<OrderInfo> orderInfoQueryWrapper = new QueryWrapper<>();
        orderInfoQueryWrapper.orderByDesc("updateDate");

        return baseMapper.selectList(orderInfoQueryWrapper);

    }

    //对参数进行更新
    @Override
    public void updateOrderInfo(String orderNo,OrderStatus strings) {

        QueryWrapper<OrderInfo> orderInfoQueryWrapper = new QueryWrapper<>();
        orderInfoQueryWrapper.eq("orderNum",orderNo);

        OrderInfo orderInfo = new OrderInfo();
        orderInfo.setOrderStatus(strings.getType());
        baseMapper.update(orderInfo,orderInfoQueryWrapper);
    }
    //获取订单状态
    @Override
    public String getOrderInfoStatus(String on)
    {
        QueryWrapper<OrderInfo> orderInfoQueryWrapper =new QueryWrapper<>();
        orderInfoQueryWrapper.eq("orderNum",on);
        OrderInfo orderInfo = baseMapper.selectOne(orderInfoQueryWrapper);
        if(orderInfo==null)
            return null;
        return  orderInfo.getOrderStatus();

    }
    //未支付
    @Override
    public List<OrderInfo> getOrdernopayStatus()
    {
        QueryWrapper<OrderInfo> orderInfoQueryWrapper =new QueryWrapper<>();
        orderInfoQueryWrapper.eq("orderStatue",OrderStatus.NOTPAY.getType());
        List<OrderInfo> orderInfo = baseMapper.selectList(orderInfoQueryWrapper);
        if(orderInfo==null)
            return null;
        return  orderInfo;

    }
    @Override
    public List<OrderInfo> getNoPayOrderByStatuNOTPAY(int minuts)
    {
        Instant instant=Instant.now().minus(Duration.ofMinutes(minuts));
        QueryWrapper<OrderInfo> orderInfoQueryWrapper=new QueryWrapper<>();
        orderInfoQueryWrapper.eq("orderStatue",OrderStatus.NOTPAY.getType());
        orderInfoQueryWrapper.le("orderDate",instant);

        return baseMapper.selectList(orderInfoQueryWrapper);
    }

    @Override
    public List<OrderInfo> getNoPayOrderByStatuREFUND_PROCESSING(int minuts)
    {
        Instant instant=Instant.now().minus(Duration.ofMinutes(minuts));
        QueryWrapper<OrderInfo> orderInfoQueryWrapper=new QueryWrapper<>();
        orderInfoQueryWrapper.eq("orderStatue",OrderStatus.REFUND_PROCESSING.getType());
        orderInfoQueryWrapper.le("orderDate",instant);

        return baseMapper.selectList(orderInfoQueryWrapper);
    }

     /**
     * 根据订单号获取订单
     * * @param orderNo
     * * @return
     * */
    @Override
    public OrderInfo getOrderByOrderNo(String orderNo)
    {
        QueryWrapper<OrderInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("orderNum", orderNo);
        OrderInfo orderInfo = baseMapper.selectOne(queryWrapper);
        return orderInfo;
    }
}
